﻿Imports System.IO
Imports System.Xml

Namespace AdvanceWars.Data
    ''' <summary>
    ''' 存储基础数据
    ''' </summary>
    <Serializable()>
    Public Class RomData

        ''' <summary>
        ''' All Units
        ''' </summary>
        Public Units As Factory(Of UnitType)

        ''' <summary>
        ''' All Weapons
        ''' </summary>
        Public Weapons As Factory(Of WeaponType)

        ''' <summary>
        ''' All Terrains
        ''' </summary>
        Public Terrains As Factory(Of TerrainType)

        Shared Function Load() As RomData
            Return LoadXml(Of RomData)("filename")
        End Function

        Private Shared Function LoadXml(Of T)(ByVal fileName As String) As T
            Dim result As T
            Dim ser As New XmlSerializer(Of T)
            Using fs As New FileStream(fileName, FileMode.Open, FileAccess.Read)
                result = ser.Desrialize(fs)
                fs.Close()
            End Using
            Return result
        End Function

        Public Sub SaveXml(ByVal fileName As String)
            'LoadDamageChart()
            'LoadMoveCostChart()
            Dim ser As New XmlSerializer(Of RomData)
            Using fs As New FileStream(fileName, FileMode.Create, FileAccess.Write)
                ser.Serialize(fs, Me)
                fs.Close()
            End Using
        End Sub
    End Class
End Namespace